package com.mjh.zhujianju.controller;

import com.google.common.collect.Maps;
import com.livew.framework.exception.XTBusinessException;
import com.mjh.zhujianju.enums.PermissionEnum;
import com.mjh.zhujianju.model.set.AnnouncementEO;
import com.mjh.zhujianju.model.set.BanersEO;
import com.mjh.zhujianju.model.set.NavigationEO;
import com.mjh.zhujianju.service.set.NavigationService;
import com.mjh.zhujianju.vo.agency.*;
import com.mjh.zhujianju.vo.base.SysUserVO;
import com.mjh.zhujianju.service.agency.*;
import com.mjh.zhujianju.service.set.AnnouncementService;
import com.mjh.zhujianju.service.set.BanersService;
import com.mjh.zhujianju.service.set.PermissionService;
import com.mjh.zhujianju.util.ApiConstant;
import com.mjh.zhujianju.util.JsonResult;
import com.mjh.zhujianju.enums.BanersEnum;
import com.mjh.zhujianju.vo.set.AnnouncementVO;
import com.mjh.zhujianju.vo.set.BanersVO;
import com.mjh.zhujianju.vo.set.NavigationVO;
import com.mjh.zhujianju.vo.set.PermissionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author liuyang
 * @Description: 首页信息
 * @date 2018/8/8 20:53
 */
@RestController
@RequestMapping("/index")
@Api("1、WX->首页")
public class IndexController {

    @Autowired
    private AnnouncementService announcementService;

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private DeptUserService deptUserService;

    @Autowired
    private BanersService banersService;

    @Autowired
    private PolicyService policyService;

    @Autowired
    private WorkDynamicService workDynamicService;

    @Autowired
    private RemoveService removeService;

    @Autowired
    private WorkGuideSercice workGuideSercice;

    @Autowired
    private NavigationService navigationService;


    @ApiOperation(value="1-1 首页", notes="json格式根据类型节点获取对应的数据")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> index (){
        JsonResult result = new JsonResult();
        Map<String,List<Object>> map = Maps.newHashMap();
        try {
        	
            //轮播图
            List<BanersVO> banersEOList = banersService.queryBanersList(BanersEnum.BANERS_SITE_INDEX.getCode());
            map.put("baners", Arrays.asList(banersEOList));
            
            //公告
            List<AnnouncementEO> announcementEOList = announcementService.queryAnnouncementNewOne();
            map.put("announcement",Arrays.asList(announcementEOList));

            //菜单
            List<NavigationVO> menus = navigationService.selectNavigationList(PermissionEnum.BANERS_SITE_INDEX.getCode());
            map.put("menu",Arrays.asList(menus));

            //政策
            List<PolicyVO> policyList = policyService.queryPolicyIndexList();
            map.put("policy",Arrays.asList(policyList));

            //动态
            List<WorkDynamicVO> workDynamicList = workDynamicService.queryWorkDynamicIndex();
            map.put("work_dynamic",Arrays.asList(workDynamicList));

            result.setResult(map);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="1-2 公告-列表", notes="公告列表")
    @RequestMapping(value = "/announcement", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getAnnouncement (){
        JsonResult result = new JsonResult();
        try {
            List<AnnouncementVO> list = announcementService.queryAnnouncementList();
            result.setResult(list);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="1-2 公告-详情", notes="根据公告id查询公告详情信息")
    @ApiImplicitParam(name = "id", value = "公告id", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/announcement/{id}", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getUserById (@PathVariable(value = "id") Long id){
        JsonResult result = new JsonResult();
        try {
            AnnouncementVO announcement = announcementService.queryAnnouncementInfo(id);
            result.setResult(announcement);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="1-3 机构介绍", notes="机构的详细信息")
    @RequestMapping(value = "/organization", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getOrganization (){
        JsonResult result = new JsonResult();
        try {
            OrganizationVO organizationVO = organizationService.getInfo();
            result.setResult(organizationVO);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="1-4 部门->列表", notes="展示所有的部门信息")
    @RequestMapping(value = "/department", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getDepartment (){
        JsonResult result = new JsonResult();
        try {
            List<DepartmentVO> list = departmentService.queryDepartmentForList();
            result.setResult(list);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="1-4 部门->详情", notes="展示部门信息详细信息，默认先查询-基本职能")
    @RequestMapping(value = "/department/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "部门id", required = true, dataType = "Long", paramType = "path")
    public ResponseEntity<JsonResult> getDepartmentById (@PathVariable(value = "id") Long id){
        JsonResult result = new JsonResult();
        try {
            DepartmentVO department = departmentService.queryDepartmentInfo(id);
            result.setResult(department);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="1-4 部门->详情->人员详情", notes="展示人员详细信息")
    @RequestMapping(value = "/department/user/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Long", paramType = "path")
    public ResponseEntity<JsonResult> getDepartmentByUsers (@PathVariable(value = "id") Long id){
        JsonResult result = new JsonResult();
        try {
            SysUserVO sysUserVO = deptUserService.querySysUserInfo(id);
            result.setResult(sysUserVO);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (XTBusinessException e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="1-5 政策->列表", notes="展示所有政策信息列表")
    @RequestMapping(value = "/policy/{type}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "type", value = "政策类型", required = true, dataType = "String", paramType = "path")
    public ResponseEntity<JsonResult> getPolicy (@PathVariable(value = "type") String type){
        JsonResult result = new JsonResult();
        try {
            //等于0查询全部
            if (type.equals("0")){
                type = null;
            }
            List<PolicyVO> list = policyService.queryPolicyList(type);
            result.setResult(list);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }


    @ApiOperation(value="1-5 政策->详情", notes="根据政策id查询政策详细信息")
    @RequestMapping(value = "/policy/detail/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "政策id", required = true, dataType = "Long", paramType = "path")
    public ResponseEntity<JsonResult> getPolicy (@PathVariable(value = "id") Long id){
        JsonResult result = new JsonResult();
        try {
            PolicyVO policyVO = policyService.queryPolicyInfo(id);
            result.setResult(policyVO);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="1-6 拆迁->列表", notes="展示所有拆迁户的信息列表")
    @RequestMapping(value = "/remove/", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getRemove (){
        JsonResult result = new JsonResult();
        try {
            List<RemoveVO> list = removeService.queryRemoveList();
            result.setResult(list);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="1-6 拆迁->详情", notes="根据拆迁户id查询详细信息")
    @RequestMapping(value = "/remove/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "拆迁id", required = true, dataType = "Long", paramType = "path")
    public ResponseEntity<JsonResult> getRemoveInfo (@PathVariable(value = "id") Long id){
        JsonResult result = new JsonResult();
        try {
            RemoveVO removeVO = removeService.getInfo(id);
            result.setResult(removeVO);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (XTBusinessException e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="1-7 工作动态->列表", notes="展示所有部门的工作动态")
    @RequestMapping(value = "/workDynamic/dept/{deptId}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "deptId", value = "部门id", required = true, dataType = "Long", paramType = "path")
    public ResponseEntity<JsonResult> getWorkDynamic (@PathVariable(value = "deptId") Long deptId){
        JsonResult result = new JsonResult();
        try {
            List<WorkDynamicVO> list = workDynamicService.queryWorkDynamicList(deptId);
            result.setResult(list);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="1-7 工作动态->详情", notes="根据工作动态id查询详情信息")
    @RequestMapping(value = "/workDynamic/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "工作动态id", required = true, dataType = "Long", paramType = "path")
    public ResponseEntity<JsonResult> getWorkDynamicInfo (@PathVariable(value = "id") Long id){
        JsonResult result = new JsonResult();
        try {
            WorkDynamicVO workDynamicVO = workDynamicService.queryWorkDynamicInfo(id);
            result.setResult(workDynamicVO);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="1-8 办事指南->列表", notes="展示所有办事指南列表")
    @RequestMapping(value = "/workGuide", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getWorkGuide (){
        JsonResult result = new JsonResult();
        try {
            Map<String,List<WorkGuideVO>> map = workGuideSercice.queryWorkGuideList();
            result.setResult(map);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value="1-8 办事指南->详情", notes="根据办事指南id查询详情信息")
    @RequestMapping(value = "/workGuide/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "办事指南id", required = true, dataType = "Long", paramType = "path")
    public ResponseEntity<JsonResult> getWorkGuideInfo (@PathVariable(value = "id") Long id){
        JsonResult result = new JsonResult();
        try {
            WorkGuideVO workGuideVO = workGuideSercice.queryWorkGuideInfo(id);
            result.setResult(workGuideVO);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }






}
